import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def send_email(from_addr, password,to_addr, subject, content, attachment_files=[]):
    msg=MIMEMultipart()
    msg['From']=from_addr
    msg['To']=to_addr
    msg['Subject']=Header(subject,'utf-8')
    msg.attach(MIMEText(content,'plain','utf-8'))
    for f in attachment_files:
        att=MIMEText(open(f,'rb').read(),'base64','utf-8')
        att['Content-Type']='application/octet-stream'
        att['Content-Disposition']='attachment;filename="%s"'%f
        msg.attach(att)
    smtp_server='smtp.qq.com'
    with smtplib.SMTP_SSL(smtp_server,465) as smtp:
        smtp.login(from_addr,password)
        smtp.sendmail(from_addr,to_addr,msg.as_string())
        smtp.quit()
        print('发送成功')
if __name__ == '__main__':
    from_addr='340143745@qq.com'
    password='jxkphfbmbsmybiib'
    to_addr='340143745@qq.com'
    html='''
    <html>
    <head></head>
    <body>
    <h1>Ciallo</h1>
    <p>Python 邮件发送测试...</p>
    </body>
    </html>
    '''
    attach=['./test.txt']
    send_email(from_addr,password,to_addr,'Python邮件测试',html,attach)
